package org.example.demo2minio.autoconfigure;

import io.minio.MinioClient;
import org.example.demo2minio.properties.MinioProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName MinioAutoConfiguration
 * @Description TODO
 * @Author Kx
 * @Date 2025/9/15 15:25
 * @Version 1.0
 */
// 标识为配置类
@Configuration
@ConditionalOnClass(MinioClient.class)
@EnableConfigurationProperties(MinioProperties.class)
public class MinioAutoConfiguration {
    @Autowired
    private MinioProperties minioProperties;

    /**
     * @author: kx
     * @date: 2025/9/16 14:57
     * @return io.minio.MinioClient
     * @description: 初始化minio客户端并纳入IOC容器管理
    */
    @Bean
    // 如果用户没有定义MinioClient则初始化
    @ConditionalOnClass
    public MinioClient minioClient() {
        if (minioProperties.getHost().isEmpty() || minioProperties.getAccount().isEmpty() || minioProperties.getPassword().isEmpty()) {
            throw new IllegalArgumentException("请检查Minio配置信息");
        }

        return MinioClient.builder()
                .endpoint(minioProperties.getHost())
                .credentials(minioProperties.getAccount(), minioProperties.getPassword())
                .build();
    }
}
